import { login, getUserInfo, getMypermissions } from '@/api/user'
import router from '@/router'

export default {
  namespaced: true,
  state: {
    token: localStorage.getItem('fcTOken') || '',
    userinfo: {}
  },
  mutations: {
    setToken(state, data) {
      state.token = data
    },
    delToken(state) {
      state.token = ''
    },
    setUserInfo(state, userinfo) {
      state.userinfo = { ...userinfo }
    },
    removeUserInfo(state) {
      state.userinfo = { }
    }
  },
  actions: {
    async loginFn(store, data) {
      const res = await login(data)
      store.commit('setToken', res.jwt)
      localStorage.setItem('fcTOken', res.jwt)
      router.push('/')
    },
    async logout(store) {
      store.commit('delToken')
      localStorage.removeItem('fcTOken')
    },
    async getUserInfo(store) {
      const res = await getUserInfo()
      console.log(35, res)
      const res2 = await getMypermissions()
      console.log(37, res2)
      store.commit('setUserInfo', { ...res, ...res2 })
    }
  }
}

